package cn.linkai.star.graph.comfyui.client.handler;


import cn.hutool.json.JSONUtil;
import cn.linkai.star.graph.comfyui.client.pojo.ComfyuiTask;
import cn.linkai.star.graph.comfyui.client.pojo.MessageBase;
import cn.linkai.star.graph.core.service.ComfuiMessageService;
import com.alibaba.fastjson2.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

public class ComfyuiMessageHandler extends TextWebSocketHandler {

    @Autowired
    private ComfuiMessageService comfuiMessageService;

    /**
     * 接受文本消息
     * @param session
     * @param message
     * @throws Exception
     */
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message){
        String payload = message.getPayload ();
        System.out.println ("==============接收到消息：" + payload);
        MessageBase messageBase = JSON.parseObject (payload, MessageBase.class);
        comfuiMessageService.handleMessage (messageBase);
    }

    /**
     * 判断是否连接成功
     * @param session
     * @throws Exception
     */
    @Override
    public void afterConnectionEstablished(WebSocketSession session){
        System.out.println ("==============连接成功" + session.getId ());
    }
}
